/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package swym.server;

import swym.server.file.*;

import java.util.*;
import swym.server.events.*;
import swym.protocol.*;

/**
 *
 * @author mschaefer
 */
public class User implements ClientListener
{
    private String name;
    private final HashMap<String, Client> clients;
    private final FileActionManager fileActionManager;
    
    public User( String name )
    {
        this.name = name;
        
        clients = new HashMap<String, Client>();
        
        fileActionManager = new FileActionManager();
    }
    
    public void addClient( Client client )
    {
        clients.put( client.getName(), client );
        fileActionManager.addClient( client );
        
        client.addClientListener( this );
    }
    
    public String getName() { return name; }
    
    public Vector<Client> getClients() 
    { 
        synchronized ( clients )
        {
            return new Vector<Client>( clients.values() );
        }
    }
    
    public Client getClient( String clientName ) 
    { 
        synchronized ( clients )
        {
            return clients.get( clientName );
        }
    }
    
    
    /**
     * Returns references to all of the clients currently
     * connected to the server.
     * @return
     */
    public Vector<Client> getConnectedClients()
    {
        Vector<Client> connectedClients = new Vector<Client>();
        
        synchronized( clients )
        {
            for ( Client client : getClients() )
            {
                if ( client.isConnected() )
                    connectedClients.add( client );
            }
        }
        
        return connectedClients;
    }
    
    public FileActionManager getFileActionManager()
    {
         return fileActionManager;
    }
    
    public void printStatus( )
    {
        System.out.println( toString() );
        
        Vector<FileAction> actions = fileActionManager.getFileActions();
        
        for ( FileAction action : actions )
            System.out.println( action );
    }
    
    public String toString()
    {
        return "user: " + name;
    }
    
    /**
     * This handler is called when one of this user's clients connects. 
     * 
     * @param connection
     * @param isFirst
     */
    public void handleNewConnection( ClientConnection connection, boolean isFirst )
    {
        
    }
}
